<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('项目名称')" prop="dealName">
        <el-input
          v-model="queryParams.dealName"
          :placeholder="$t('项目名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('负责人')" prop="personChargeName">
        <el-input
          v-model="queryParams.personChargeName"
          :placeholder="$t('负责人')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('首次划款时间')">
        <DatePicker
          v-model="dateRange"
          type="daterange"
        />
      </el-form-item>
      <!--      <el-form-item :label="$t('主体名称')" prop="personChargeName">-->
      <!--        <el-input v-model="queryParams.investName" :placeholder="$t('主体名称')" @keyup.enter.native="queryHandle" />-->
      <!--      </el-form-item>-->
      <el-form-item v-if="queryParams.dealType === 'bf'" :label="$t('管理公司')" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          :placeholder="$t('管理公司')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t('导出') }}
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" :show-summary="true" :summary-method="getSummaries" border>
      <el-table-column :label="$t('序号')" align="center" show-overflow-tooltip min-width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('项目名称')" align="center" prop="dealName" show-overflow-tooltip min-width="180">
        <template slot-scope="scope">
          <router-link
            target="_blank"
            :to="{ path:'/deal/home/dealBaseHome', query: { id: scope.row.id, dealType: scope.row.dealType, companyId: scope.row.companyId, menuKey: 'deal_after' }}"
            class="link_a"
          >{{ scope.row.dealName }}</router-link>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.dealType === 'bf'" :label="$t('管理公司')" align="center" prop="companyName" show-overflow-tooltip min-width="180" />
      <el-table-column :label="$t('项目负责人')" align="center" prop="personChargeName" show-overflow-tooltip min-width="100" />
      <!-- 参股基金期限 -->
      <el-table-column v-if="queryParams.dealType === 'bf'" :label="$t('期限')" align="center" prop="bfFundTerm" show-overflow-tooltip min-width="100" />
      <el-table-column v-if="queryParams.dealType === 'bf'" :label="$t('基金规模') + $t('金额单位')" header-align="center" align="right" prop="subscribeScale" show-overflow-tooltip min-width="100">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.subscribeScale, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.dealType === 'debt'" :label="$t('年化收益(%)')" align="center" prop="incomeRate" show-overflow-tooltip min-width="100" />
      <!-- 债权期限 -->
      <el-table-column v-if="queryParams.dealType === 'debt'" :label="$t('期限')" align="center" prop="debtPeriod" show-overflow-tooltip min-width="100" />
      <el-table-column v-if="queryParams.dealType === 'debt'" :label="$t('发行主体')" align="center" prop="issued" show-overflow-tooltip min-width="150" />
      <el-table-column v-if="queryParams.dealType === 'equity'" :label="$t('聚焦行业')" align="center" prop="industryFocus" show-overflow-tooltip min-width="150" />
      <el-table-column v-if="queryParams.dealType === 'equity'" :label="$t('持股比例(%)')" align="center" prop="shareholdingRation" show-overflow-tooltip min-width="100">
        <template slot-scope="scope">
          {{ numToFixed(scope.row.shareholdingRation) }}
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.dealType === 'bf'" :label="$t('协议金额') + $t('金额单位')" header-align="center" align="right" prop="agreementMoney" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.agreementMoney, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('首次划款时间')" align="center" prop="dateInvestmentFact" show-overflow-tooltip min-width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateInvestmentFact) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已付款金额') + $t('金额单位')" header-align="center" align="right" prop="amountInvestmentFact" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.amountInvestmentFact, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已回收金额') + $t('金额单位')" header-align="center" align="right" prop="payBack" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.payBack, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已回收本金') + $t('金额单位')" header-align="center" align="right" prop="returnPrincipal" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.returnPrincipal, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('已回收收益') + $t('金额单位')" header-align="center" align="right" prop="returnIncome" show-overflow-tooltip min-width="120">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.returnIncome, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="queryParams.dealType !== 'estate'" :label="$t('最新估值') + $t('金额单位')" header-align="center" align="right" prop="valuation" show-overflow-tooltip min-width="100">
        <template slot-scope="scope">
          <span class="money-text">
            {{ numToFixedToWan(scope.row.valuation) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('IRR')" align="center" prop="latestIrr" show-overflow-tooltip min-width="100">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="findHistoryIRR(scope.row)"
          >{{ scope.row.latestIrr }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" fixed="right" align="center" class-name="small-padding fixed-width" width="170">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="editLpRights(scope.row.id)"
          >{{ $t("出资人权益") }}
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-view"
            @click="dealProjectPenetrationView(scope.row)"
          >{{ $t('穿透图') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 出资人权益 -->
    <el-dialog :title="$t('出资人权益')" :visible.sync="rightsOpen" width="50%" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm">
      <fundLpRightsForm ref="fundLpRightsForm" />
    </el-dialog>

    <!-- 现金流IRR对话框 -->
    <el-dialog v-loading="irrLoading" :title="$t('IRR')" :visible.sync="irrOpen" custom-class="customDialog" width="40%" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <el-table v-loading="irrLoading" :data="dealIrrList" border max-height="600" class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('维度')" align="center" prop="dimension" min-width="259" />
        <el-table-column :label="$t('IRR')" align="center" prop="xirr" min-width="200">
          <template slot-scope="scope">
            <span><el-link type="primary" @click="findXIrrDetails(scope.row)">{{ moneyFormat(scope.row.xirr, undefined, 2, true) }}</el-link></span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- xIrr计算明细对话框 -->
    <el-dialog :title="$t('计算明细')" :visible.sync="xIrrDetailsOpen" :close-on-click-modal="false" :lock-scroll="false" :width="'50%'" custom-class="customDialog" append-to-body>
      <xIrrBaseDetailsList ref="xIrrBaseDetailsList" class="dialog-content" />
      <div slot="footer" class="dialog-footer mt-15" style="text-align: right;">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportXIrrHandle"
        >{{ $t('导出') }}
        </el-button>
      </div>
    </el-dialog>

    <el-dialog v-if="forceComponentOpen" :title="$t('项目穿透图')" :visible.sync="forceComponentOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <forceComponent ref="forceComponent" :bus-data="forceData" />
    </el-dialog>
  </div>
</template>

<script>

import { getFundPortfolioSum, queryFundPortfolio } from '@/api/deal/projectInvestmentSituation/projectInvestmentSituation'
import fundLpRightsForm from '@/views/fund/lpRights/fundLpRightsForm'
import forceComponent from '@/components/Deal/forceComponent.vue'
import { ruoyiFloat } from '@/utils/ruoyi'
import { getDealInvestmentIrrHistory } from '@/api/deal/investment/dealInvestment'
import xIrrBaseDetailsList from '@/components/Xirr/details/xirrBaseDetailsList'
export default {
  components: {
    fundLpRightsForm,
    forceComponent,
    xIrrBaseDetailsList
  },
  data() {
    return {
      total: 0,
      loading: false,
      irrLoading: false,
      // 出资人权益页面
      rightsOpen: false,
      // IRR对应的现金流明细页面
      irrCashFlowOpen: false,
      irrOpen: false,
      xIrrDetailsOpen: false,
      dealIrrList: [],
      queryParams: {
        fundId: this.$route.query.id,
        dealType: '',
        dealName: undefined,
        personChargeName: undefined,
        companyName: undefined,
        investName: undefined,
        pageNum: 1,
        pageSize: 10
      },
      dataList: [],
      dataListSum: {},
      forceComponentOpen: false,
      forceData: undefined,
      dateRange: []
    }
  },
  created() {
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      if (this.$route.params.dealType) {
        this.queryParams.dealType = this.$route.params.dealType
      }
      getFundPortfolioSum(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataListSum = response.data
      })
      queryFundPortfolio(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.dataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.companyName = ''
      this.queryParams.personChargeName = ''
      this.queryParams.dealName = ''
      this.queryParams.investName = ''
      this.queryHandle()
    },
    /** 导出按钮操作 */
    exportHandle() {
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.downLoadExcel('/deal/projectInvestmentSituation/queryFundPortfolio/export', this.queryParams)
      }).catch(() => {
      })
    },
    // 修改出资人权益
    editLpRights(dealId) {
      this.rightsOpen = true
      this.$nextTick(() => {
        this.$refs.fundLpRightsForm.init(this.$route.query.id, dealId)
      })
    },
    findHistoryIRR(row) {
      if (row.dealInvestmentId) {
        this.irrOpen = true
        this.irrLoading = true
        this.dealIrrList = []
        getDealInvestmentIrrHistory({ id: row.dealInvestmentId }).then(response => {
          this.dealIrrList = response.rows
        })
        this.irrLoading = false
      }
    },
    findXIrrDetails(row) {
      this.xIrrDetailsOpen = true
      this.$nextTick(() => {
        this.$refs.xIrrBaseDetailsList.init(row.id)
      })
    },
    exportXIrrHandle() {
      this.$nextTick(() => {
        this.$refs.xIrrBaseDetailsList.exportHandle()
      })
    },
    dealProjectPenetrationView(row) {
      this.forceData = { busId: row.companyId, busType: 'company', busName: row.companyName }
      this.forceComponentOpen = true
    },
    numToFixed(val) {
      if (!isNaN(val)) {
        return ruoyiFloat().formatNum(val).toFixed(2)
      } else {
        return val
      }
    },
    numToFixedToWan(val) {
      console.log(val)
      if (!isNaN(val)) {
        var moneyFormat = this.moneyFormat(val, 'w')
        return ruoyiFloat().formatNum(moneyFormat.replaceAll(',', '')).toFixed(2)
      } else {
        return val
      }
    },
    // 合计
    getSummaries(param) {
      const { columns } = param
      const sums = []
      columns.forEach((column, index) => {
        if (this.queryParams.dealType === 'equity') {
          if (index === 0) {
            sums[index] = '合计'
          } else if (index === 6) {
            sums[index] = this.moneyFormat(this.dataListSum.amountInvestmentFact, 'w')
          } else if (index === 7) {
            sums[index] = this.moneyFormat(this.dataListSum.payBack, 'w')
          } else if (index === 8) {
            sums[index] = this.moneyFormat(this.dataListSum.returnPrincipal, 'w')
          } else if (index === 9) {
            sums[index] = this.moneyFormat(this.dataListSum.returnIncome, 'w')
          } else if (index === 10) {
            sums[index] = this.numToFixedToWan(this.dataListSum.valuation)
          } else {
            sums[index] = ''
          }
        } else if (this.queryParams.dealType === 'debt') {
          if (index === 0) {
            sums[index] = '合计'
          } else if (index === 7) {
            sums[index] = this.moneyFormat(this.dataListSum.amountInvestmentFact, 'w')
          } else if (index === 8) {
            sums[index] = this.moneyFormat(this.dataListSum.payBack, 'w')
          } else if (index === 9) {
            sums[index] = this.moneyFormat(this.dataListSum.returnPrincipal, 'w')
          } else if (index === 10) {
            sums[index] = this.moneyFormat(this.dataListSum.returnIncome, 'w')
          } else if (index === 11) {
            sums[index] = this.numToFixedToWan(this.dataListSum.valuation)
          } else {
            sums[index] = ''
          }
        } else if (this.queryParams.dealType === 'bf') {
          if (index === 0) {
            sums[index] = '合计'
          } else if (index === 5) {
            sums[index] = this.moneyFormat(this.dataListSum.subscribeScale, 'w')
          } else if (index === 6) {
            sums[index] = this.moneyFormat(this.dataListSum.agreementMoney, 'w')
          } else if (index === 8) {
            sums[index] = this.moneyFormat(this.dataListSum.amountInvestmentFact, 'w')
          } else if (index === 9) {
            sums[index] = this.moneyFormat(this.dataListSum.payBack, 'w')
          } else if (index === 10) {
            sums[index] = this.moneyFormat(this.dataListSum.returnPrincipal, 'w')
          } else if (index === 11) {
            sums[index] = this.moneyFormat(this.dataListSum.returnIncome, 'w')
          } else if (index === 12) {
            sums[index] = this.numToFixedToWan(this.dataListSum.valuation)
          } else {
            sums[index] = ''
          }
        } else if (this.queryParams.dealType === 'estate') {
          if (index === 0) {
            sums[index] = '合计'
          } else if (index === 4) {
            sums[index] = this.moneyFormat(this.dataListSum.amountInvestmentFact, 'w')
          } else if (index === 5) {
            sums[index] = this.moneyFormat(this.dataListSum.payBack, 'w')
          } else if (index === 6) {
            sums[index] = this.moneyFormat(this.dataListSum.returnPrincipal, 'w')
          } else if (index === 7) {
            sums[index] = this.moneyFormat(this.dataListSum.returnIncome, 'w')
          } else if (index === 8) {
            sums[index] = this.numToFixedToWan(this.dataListSum.valuation)
          } else {
            sums[index] = ''
          }
        }
      })
      return sums
    }
  }
}
</script>

<style scoped>

</style>
